<?php
/**
 * User: xuhong
 * Date: 2018/10/30
 */

namespace extend\wechat\mp;

class Material
{
    /**
     * 上传临时素材
     * @param $file
     * @param $type [image,voice,video,thumb]
     * @param $append
     * @return mixed
     */
    public static function uploadLimit($file, $type, $append = [])
    {
        $access_token = Voucher::accessToken()['access_token'];

        // 临时素材链接
        $url = "http://file.api.weixin.qq.com/cgi-bin/media/upload?access_token={$access_token}&type={$type}";
        if (class_exists('\CURLFile')) {
            $data = [
                'media' => new \CURLFile(realpath($file)),
            ];
        } else {
            $data = [
                'media' => '@' . $file,
            ];
        }
        $data = array_merge($data, $append);
        return postRequest($url, $data);
    }


    /**
     * 上传永久素材
     * @param $file
     * @param $type [image,voice,video,thumb]
     * @param $append
     * @return mixed
     */
    public static function uploadPermanent($file, $type, $append = [])
    {
        $access_token = Voucher::accessToken()['access_token'];

        // 永久素材链接
        $url = "https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={$access_token}&type={$type}";
        if (class_exists('\CURLFile')) {
            $data = [
                'media' => new \CURLFile(realpath($file)),
            ];
        } else {
            $data = [
                'media' => '@' . $file,
            ];
        }
        $data = array_merge($data, $append);
        return postRequest($url, $data);
    }

    /**
     * 添加图文素材
     * @param $data
     * @return mixed
     */
    public static function uploadArticle($data)
    {
        $access_token = Voucher::accessToken()['access_token'];

        $url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token={$access_token}";
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        return postRequest($url, $data);
    }

    /**
     * 获取图文信息
     * @param $media_id
     * @return mixed
     */
    public static function getArticleInfo($media_id)
    {
        $access_token = Voucher::accessToken()['access_token'];

        $url = "https://api.weixin.qq.com/cgi-bin/material/get_material?access_token={$access_token}";
        $data['media_id'] = $media_id;
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        return postRequest($url, $data);
    }


    /**
     * 修改永久素材
     * @param $media_id
     * @param $index
     * @param $content_data
     * @return mixed
     */
    public static function updateMaterial($media_id, $index, $content_data)
    {
        $access_token = Voucher::accessToken()['access_token'];

        $url = "https://api.weixin.qq.com/cgi-bin/material/update_news?access_token={$access_token}";
        $data = $content_data;
        $data['media_id'] = $media_id;
        $data['index'] = $index;
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        return postRequest($url, $data);
    }

    /**
     * 删除素材
     * @param $media_id
     * @return mixed
     */
    public static function deleteMaterial($media_id)
    {
        $access_token = Voucher::accessToken()['access_token'];

        $url = "https://api.weixin.qq.com/cgi-bin/material/del_material?access_token={$access_token}";
        $data = [
            'media_id' => $media_id
        ];
        $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        return postRequest($url, $data);
    }

}